#include "adspage.h"
#include "ui_adspage.h"
#include "customwidget.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlDriver>

Adspage::Adspage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Adspage)
{
    ui->setupUi(this);

    QSqlQuery query;
    query.exec("SELECT \"Type_of_rent\".\"Value\", \"Real_estate\".\"Rooms\", \"Real_estate\".\"Area\", \"Real_estate\".\"Price\", \"Client\".\"Name\", \"Client\".\"Surname\", \"Client\".\"Patronymic\", \"Client\".\"Phone_number\" from \"Real_estate\", \"Type_of_rent\", \"Client\" WHERE \"Real_estate\".\"Type_of_rent_id\" = \"Type_of_rent\".\"Id\" AND \"Real_estate\".\"Client_id\" = \"Client\".\"Id\"");
    //query.exec("SELECT \"Real_estate\".\"Title\", \"Real_estate\".\"Price\", \"Real_estate\".\"Description\", \"Type_of_rent\".\"Value\" FROM \"Real_estate\", \"Type_of_rent\" WHERE \"Real_estate\".\"type_of_rent_id\" = \"Type_of_rent\".\"Id\"");

    while(query.next())
    {
        auto item = new QListWidgetItem();
        auto widget = new CustomWidget(this);

        widget->setText(QString(query.value(0).toString()), QString(query.value(1).toString()), QString(query.value(2).toString()), QString(query.value(3).toString()), QString(query.value(4).toString()), QString(query.value(5).toString()), QString(query.value(6).toString()), QString(query.value(7).toString()));

        item->setSizeHint(widget->sizeHint());

        ui->listWidget->addItem(item);
        ui->listWidget->setItemWidget(item, widget);
    }
}

Adspage::~Adspage()
{
    delete ui;
}


void Adspage::removeItem(const QString &title) {

    for (int i = 0; i < ui->listWidget->count(); ++i) {
        auto item = ui->listWidget->item(i);
        auto itemWidget = dynamic_cast<CustomWidget*>(ui->listWidget->itemWidget(item));
        if (itemWidget->getText() == title){
            delete item;
            break;
        }
    }
}

void Adspage::on_addbutton_clicked()
{
    emit editadspageshow();
}


void Adspage::on_update_clicked()
{
    QSqlQuery query;
    query.exec("SELECT \"Type_of_rent\".\"Value\", \"Real_estate\".\"Rooms\", \"Real_estate\".\"Area\", \"Real_estate\".\"Price\", \"Client\".\"Name\", \"Client\".\"Surname\", \"Client\".\"Patronymic\", \"Client\".\"Phone_number\" from \"Real_estate\", \"Type_of_rent\", \"Client\" WHERE \"Real_estate\".\"Type_of_rent_id\" = \"Type_of_rent\".\"Id\" AND \"Real_estate\".\"Client_id\" = \"Client\".\"Id\"");
    //query.exec("SELECT \"Real_estate\".\"Title\", \"Real_estate\".\"Price\", \"Real_estate\".\"Description\", \"Type_of_rent\".\"Value\" FROM \"Real_estate\", \"Type_of_rent\" WHERE \"Real_estate\".\"type_of_rent_id\" = \"Type_of_rent\".\"Id\"");

    while(query.next())
    {
        auto item = new QListWidgetItem();
        auto widget = new CustomWidget(this);

        widget->setText(QString(query.value(0).toString()), QString(query.value(1).toString()), QString(query.value(2).toString()), QString(query.value(3).toString()), QString(query.value(4).toString()), QString(query.value(5).toString()), QString(query.value(6).toString()), QString(query.value(7).toString()));

        item->setSizeHint(widget->sizeHint());

        ui->listWidget->addItem(item);
        ui->listWidget->setItemWidget(item, widget);
    }
}

